from exts import db
from geopy.distance import geodesic


class InterestingPlaceModel(db.Model):
    __tablename__ = 'interesting_place'
    # id
    place_id = db.Column(db.Integer, primary_key=True,  autoincrement=True)
    # 景点名称
    place_name = db.Column(db.String(100), nullable=False)
    # 景点所在省份地址
    place_address = db.Column(db.String(100), nullable=False)
    # 景点介绍
    place_intro = db.Column(db.Text, nullable=False)
    # 缩略图
    cover_img = db.Column(db.String(100), nullable=False, default='http://127.0.0.1:5000/images/place/default.jpeg')
    # 纬度
    latitude = db.Column(db.Numeric(11, 8), nullable=False)
    # 经度
    longitude = db.Column(db.Numeric(11, 8), nullable=False)

    def to_dict(self, latitude=None, longitude=None):
        place = {c.name: getattr(self, c.name, None) for c in self.__table__.columns}
        place['distance'] = 0
        if latitude and longitude:
            place['distance'] = geodesic((latitude, longitude), (place['latitude'],place['longitude'])).km
        return place
